<?php

class PostModel extends CI_Model {

	public $articles_pictures;

	public function __construct() {
		// Call the Model constructor
		parent::__construct();
		$this -> articles_pictures = realpath(APPPATH . "../post_images");

	}

	public function getById($id = NULL) {

		if ($id != NULL) {
			$this -> db -> where('id', $id);
			$query = $this -> db -> get('blog_post');
			return $query -> row_array();
		}
	}

	public function getAll($per_page = NULL, $offset = NULL) {

		$query = $this -> db -> get('blog_post', $per_page, $offset);
		return $query -> result_array();

	}

	public function getLastestPost() {

		$this -> db -> order_by("date_published", "desc");
		$query = $this -> db -> get('blog_post', 1);
		return $query -> row();
	}

	public function search($term = "") {

		if ($term != NULL && strlen($term)) {

			$array = array('name' => $term);

			$this -> db -> like($array);

			$query = $this -> db -> get('blog_post');

			return $query -> result_array();

		}
	}

	public function add($data) {

		/**
		 * clear not necessary data.
		 * */
		unset($data['submit']);

		$data['enabled'] = (isset($data['enabled']) == null) ? 0 : 1;
		$data['featured'] = (isset($data['featured']) == null) ? 0 : 1;
		$data['comments_enabled'] = (isset($data['comments_enabled']) == null) ? 0 : 1;
		/**
		 * Adding the date
		 * */

		$data['date_published'] = date('Y-m-d H:i:s');

		$this -> db -> insert('blog_post', $data);
		return $this -> db -> insert_id();

	}

	public function updaterecord($data, $id) {

		$id = (int)$id;

		$data['enabled'] = (isset($data['enabled']) == null) ? 0 : 1;
		$data['featured'] = (isset($data['featured']) == null) ? 0 : 1;
		$data['comments_enabled'] = (isset($data['comments_enabled']) == null) ? 0 : 1;

		$data['date_last_update'] = date('Y-m-d H:i:s');

		$this -> db -> where('id', $id);
		return $this -> db -> update('blog_post', $data) ? TRUE : FALSE;

	}

	public function delete($id) {

		$deleted_record;

		$this -> db -> where('id', $id);
		$query = $this -> db -> get('blog_post');
		$deleted_record = $query -> row_array();

		if (is_array($deleted_record)) {
			return $deleted_record;
		}
	}

	public function do_upload($filename = NULL) {

		$config['upload_path'] = $this -> articles_pictures;
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = 2097152;
		$config['max_width'] = 2048;
		$config['max_height'] = 1536;
		$config['overwrite'] = TRUE;
		$config['file_name'] = (!$filename) ? uniqid() : $filename;

		$this -> load -> library('upload', $config);

		if (!$this -> upload -> do_upload("file")) {

			$error = array('error' => $this -> upload -> display_errors());

		} else {

			$image_data = $this -> upload -> data();

			$config['source_image'] = $image_data['full_path'];
			$config['new_image'] = $this -> articles_pictures . '/thumbs';
			$config['maintain_ration'] = TRUE;
			$config['width'] = 150;
			$config['height'] = 100;

			$this -> load -> library('image_lib', $config);
			$this -> image_lib -> resize();
			return $image_data;
		}
	}

	public function delete_file($file) {
		return delete_files($this -> articles_pictures . $file);
	}

	public function add_comment($data) {

		$participant['name'] = $data['name'];
		$participant['email'] = $data['email'];
		$participant['website'] = $data['website'];

		$participant_id = $this -> add_patticipant($participant);

		if ($participant_id) {
			$comment['post_id'] = $data['post_id'];
			$comment['comment'] = $data['comment'];
			$comment['user_id'] = $participant_id;
			$comment['enabled'] = $data['enabled'];
			$comment['date'] = date('Y-m-d H:i:s');

			$this -> db -> insert('blog_comment', $comment);
			return $this -> db -> insert_id();

		}

	}

	public function add_patticipant($data) {

		$this -> db -> insert('blog_participant', $data);
		return $this -> db -> insert_id();
	}

	public function getRelatedPostComments($post_id) {

		if ($post_id != NULL) {

			$this -> db -> select('blog_participant.name, blog_participant.email, blog_participant.website,blog_comment.id, blog_comment.comment, blog_comment.date');
			$this -> db -> where('post_id', $post_id);
			$this -> db -> from('blog_comment');
			$this -> db -> join('blog_participant', 'blog_participant.id = blog_comment.user_id');
			$query = $this -> db -> get();
			return $query -> result_array();

		} else {
			return NULL;
		}
	}

}
